<?php

declare(strict_types=1);

namespace app\customer\model;

use think\Model;

class Customer extends Model
{
    // 定义数据表名
    protected $name = 'customer';

    // 定义主键
    protected $primaryKey = 'id';

    // 定义自动时间戳
    protected $autoIncrement = true;

    // 定义创建时间字段名
    protected $createTime = 'create_time';

    // 定义更新时间字段名
    protected $updateTime = 'update_time';

    // 隐藏属性，防止原始字段被覆盖
    // protected $hidden = ['creator', 'updater'];

    // 定义数据类型
    protected $dataType = [
        'status' => 'int',
        'stage' => 'int',
        'tags' => 'string',
        'loss' => 'string',
        'curator' => 'string',
        'custype' => 'string',
        'cusnature' => 'string',
        'source' => 'string',
        'att_path' => 'string',
        'remark' => 'string',
        'is_del' => 'int',
    ];

    // 定义默认值
    protected $default = [
        'status' => 0,
        'stage' => 0,
        'tags' => '',
        'loss' => '',
        'curator' => '',
        'custype' => '',
        'cusnature' => '',
        'source' => '',
        'att_path' => '',
        'remark' => '',
        'is_del' => 0,
    ];

    // 定义关联关系
    protected $relation = [
        'creator' => ['user', 'hasOne'],
        'updater' => ['user', 'hasOne'],
    ];

    protected $status = [
        0 => '无效客户',
        1 => '成交客户',
        2 => '忠诚客户',
    ];

    protected $stage = [
        0 => '流失',
        1 => '确认需求',
        2 => '成交',
    ];

    protected $source = [
        "00" => '内部',
        "01" => '网站'
    ];

    protected $login_status = [
        0 => '锁定',
        1 => '正常'
    ];
    protected $is_login = [
        0 => '不登陆',
        1 => '登陆'
    ];



    public function creator()
    {
        return $this->belongsTo('Operator', 'creator', 'id');
    }
    public function getCreatorValueAttr($value, $data)
    {
        $creator = $this->getRelation('creator');
        if (!$creator) {
            $creator = $this->creator()->find();
        }
        return $creator ? $creator->cname : null;
    }

    public function updater()
    {
        return $this->belongsTo('Operator', 'updater', 'id');
    }
    public function getUpdaterValueAttr($value, $data)
    {
        $updater = $this->getRelation('updater');
        if (!$updater) {
            $updater = $this->creator()->find();
        }
        return $updater ? $updater->cname : null;
    }

    public function getOwnerValueAttr($value, $data)
    {
        if (is_null($data['owner']) || '' == $data['owner']) {
            return [];
        }

        $owners = Operator::where('id', $data['owner'])->field('cname')->select()->toArray();
        return implode(',', array_column($owners, 'cname'));
    }
    public function getPartnerValueAttr($value, $data)
    {
        if (is_null($data['partner']) || '' == $data['partner']) {
            return '';
        }
        $partnerIds = explode(',', $data['partner']);
        $partnerIds = array_filter($partnerIds, 'strlen');
        $partners = Operator::whereIn('id', $partnerIds)->field('cname')->select()->toArray();

        return  implode(',', array_column($partners, 'cname'));
    }
}
